package com.zhangqie.cart.adapter;

import android.content.Context;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhangqie.cart.widget.LinearLayoutForListView;
import com.zhangqie.cart.R;
import com.zhangqie.cart.entity.CartInfo;
import com.zhangqie.cart.itemclick.OnItemMoneyClickListener;
import com.zhangqie.cart.itemclick.OnViewItemClickListener;

import java.util.List;

public class CartAdapter extends BaseQuickAdapter<CartInfo.DataBean, BaseViewHolder> {

    private Context context;
    private List<CartInfo.DataBean> list;
    private boolean isCheck = false;

    public CartAdapter(Context context, List<CartInfo.DataBean> list) {
        super(R.layout.item_group, list);
        this.context = context;
        this.list = list;
    }


    @Override
    protected void convert(BaseViewHolder holder, CartInfo.DataBean item) {
        holder.setText(R.id.tv_group, item.getShop_name()).setChecked(R.id.cb_group, item.ischeck());
        ListBaseAdapter listBaseAdapter = new ListBaseAdapter(context, holder.getLayoutPosition(), item.getItems());
        LinearLayoutForListView listView = holder.getView(R.id.listview_cart);
        listView.setAdapter(listBaseAdapter);
        CheckBox cbChild = holder.getView(R.id.cb_group);
        cbChild.setOnClickListener(v -> mOnItemClickListener.onItemClick(cbChild.isChecked(), v, holder.getLayoutPosition()));

        //店铺下的checkbox
        listBaseAdapter.setOnClickListenterModel((isFlang, view, onePosition, position) -> {
            list.get(onePosition).getItems().get(position).setIscheck(isFlang);
            int length = list.get(onePosition).getItems().size();
            if (length == 1) {
                mOnItemClickListener.onItemClick(isFlang, view, onePosition);
            } else {
                for (int i = 0; i < length; i++) {
                    if (list.get(onePosition).getItems().get(i).ischeck()) {//true,true,true
                        isCheck = true;
                    } else {
                        isCheck = false;
                        break;
                    }
                }
                list.get(onePosition).setIscheck(isCheck);
                onItemMoneyClickListener.onItemClick(view, onePosition);
                notifyDataSetChanged();
            }
        });
        /***
         * 数量增加和减少
         */
        listBaseAdapter.setOnClickAddCloseListenter((view, index, onePosition, position, num) -> {
            if (index == 1) {
                if (num > 1) {
                    list.get(onePosition).getItems().get(position).setNum((num - 1));
                    notifyDataSetChanged();
                }
            } else {
                list.get(onePosition).getItems().get(position).setNum((num + 1));
                notifyDataSetChanged();
            }
            onItemMoneyClickListener.onItemClick(view, onePosition);
        });
    }

    // CheckBox全选的方法
    private OnViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    // 计算价钱
    private OnItemMoneyClickListener onItemMoneyClickListener = null;

    public void setOnItemMoneyClickListener(OnItemMoneyClickListener listener) {
        this.onItemMoneyClickListener = listener;
    }


    /**
     * 删除选中item
     */
    public void removeChecked() {
        int iMax = list.size() - 1;
        //这里要倒序，因为要删除mDatas中的数据，mDatas的长度会变化
        for (int i = iMax; i >= 0; i--) {
            if (list.get(i).ischeck()) {
                list.remove(i);
                notifyItemRemoved(i);
                notifyItemRangeChanged(i, list.size());
            } else {
                int length = list.get(i).getItems().size() - 1;
                for (int j = length; j >= 0; j--) {
                    if (list.get(i).getItems().get(j).ischeck()) {
                        list.get(i).getItems().remove(j);
                        notifyItemRemoved(i);
                        notifyItemRangeChanged(i, list.size());
                    }
                }
            }
        }
    }

}
